package com.yvdelx.cyclowomen.business;

import com.yvdelx.cyclowomen.object.Cycle;
import com.yvdelx.cyclowomen.object.Day;
import com.yvdelx.cyclowomen.object.Profile;
import com.yvdelx.cyclowomen.utility.DateUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ProfileBusiness
 * Created by ydelvaux on 06/03/14.
 */
public class ProfileBusiness {

    @SuppressWarnings("unused")
    private static Class cycleBusinessClass = ProfileBusiness.class;

    /**
     * Add a cycle to the profile
     * @param cycle Cycle
     * @param profile Profile
     */
    public static void addCycle(Cycle cycle, Profile profile){
        LogUtility.debug(cycleBusinessClass,"Cycle is adding to the current profile.");
        CycleBusiness.computeNatureDays(cycle); // Before adding cycle, it's computed
        profile.getCycleList().add(cycle);
    }

    /**
     * Get the cycle by date
     * @param date Date
     * @param profile Profile
     * @return Cycle
     */
    public static Cycle getCycleByDayDate(Date date, Profile profile){
        // Get the cycle list from the profile in param
        for(Cycle cycle : profile.getCycleList()){
            // Call cycle business to get the day by date
            Day day = CycleBusiness.getDay(date,cycle);
            // If day exist, it's the current cycle.
            if(day != null){
                CycleBusiness.computeNatureDays(cycle);
                return cycle;
            }
        }
        return null;
    }

    /**
     * Get current cycle (flag)
     * @param profile Profile
     * @return Cycle
     */
    public static Cycle getCurrentCycle(Profile profile){
        for(Cycle cycle : profile.getCycleList()){
            if(cycle.isCurrentCycle()){ // It's a variable up to date
                CycleBusiness.computeNatureDays(cycle);
                return cycle;
            }
        }
        return null;
    }

    /**
     * Return the cycles corresponding to the month and year in the date
     * @param date Date
     * @param profile Profile
     * @return List<Cycle>
     */
    public static List<Cycle> getCycles(Date date, Profile profile) {
        List<Cycle> cycles = profile.getCycleList();
        List<Cycle> result = new ArrayList<Cycle>();
        for(Cycle cycle : cycles){
            if(DateUtility.isBetween(date,cycle.getDateDebut(),cycle.getDateFin())){
                result.add(cycle);
            }
        }
        return result;
    }

    /**
     * Get the cycle by its id in the profile
     * @param id String
     * @param profile Profile
     * @return Cycle result
     */
    public static Cycle getCycleById(String id, Profile profile) {
        for(Cycle cycle : profile.getCycleList()){
            if(cycle.getIdCycle().equals(id)){
                return cycle;
            }
        }
        return null;
    }
}
